package com.googlecode.intellimars.mrsp;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.peer.PeerFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class MaveRepoSearchPlugin implements ProjectComponent {
    public static final String TOOL_WINDOW_ID = "Maven Repo Search";
    private static final Icon icon = IconLoader.getIcon("/com/googlecode/intellimars/mrsp/icon.png");

    private Project project;

    public MaveRepoSearchPlugin(Project project) {
        this.project = project;
    }

    public void initComponent() { }

    public void disposeComponent() { }

    @NotNull
    public String getComponentName() {
        return "MaveRepoSearchPlugin";
    }

    public void projectOpened() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        JPanel panel = new MavenRepoSearchView().getMainPanel();
        ToolWindow toolWindow = toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, false, ToolWindowAnchor.BOTTOM);
        ContentFactory contentFactory = PeerFactory.getInstance().getContentFactory();
        Content content = contentFactory.createContent(panel, "", false);
        toolWindow.getContentManager().addContent(content);
        toolWindow.setIcon(icon);
    }

    public void projectClosed() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        toolWindowManager.unregisterToolWindow(TOOL_WINDOW_ID);
    }
}
